package com.kyhd.audio.redis;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import lombok.Getter;

@Repository
public class CodeRedis {
	
	@Getter
	private RedisTemplate<String, Object> redisTemplate;

	@Autowired
	public CodeRedis(RedisTemplate<String, Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	//String
	public void setKey(String key, String value) {
		ValueOperations<String, Object> ops = this.redisTemplate.opsForValue();
		ops.set(key, value);
	}

	public void setKeyBySecond(String key, String value, long timeout) {
		ValueOperations<String, Object> ops = this.redisTemplate.opsForValue();
		ops.set(key, value, timeout, TimeUnit.SECONDS);
	}

	public void setKeyByMinutes(String key, String value, long timeout) {
		ValueOperations<String, Object> ops = this.redisTemplate.opsForValue();
		ops.set(key, value, timeout, TimeUnit.MINUTES);
	}


	public void setKeyByHour(String key, String value, long timeout) {
		ValueOperations<String, Object> ops = this.redisTemplate.opsForValue();
		ops.set(key, value, timeout, TimeUnit.HOURS);
	}
	
	public void setKeyByDay(String key, String value, long timeout) {
		ValueOperations<String, Object> ops = this.redisTemplate.opsForValue();
		ops.set(key, value, timeout, TimeUnit.DAYS);
	}

	public Object getValue(String key) {
		ValueOperations<String, Object> ops = this.redisTemplate.opsForValue();
		return ops.get(key);
	}
	
	public Set<String> getAll(String prestr) {
		Set<String> keys = this.redisTemplate.keys(prestr+"*");
		return keys;
	}
	public void deleteKey(String key) {
		this.redisTemplate.delete(key);
	}
	
	//List
	public void putList(String key,Object[] args) {
		this.redisTemplate.delete(key);
		for (Object s : args) {
			this.redisTemplate.opsForList().rightPush(key,s);
		}
	}
	
	public void putListByDirection(String key, Object value, boolean direction) {
		if(direction) {
			this.redisTemplate.opsForList().rightPush(key,value);
		}else {
			this.redisTemplate.opsForList().leftPush(key,value);
		}
	}
	
	public void putListByIndex(String key, int index, String newValue) {
		this.redisTemplate.opsForList().set(key, index, newValue);
	}
	
	public void deleteListByNum(String key, int num, String value) {
        redisTemplate.opsForList().remove(key, num, value);
	}
	
	public List<Object> getList(String key) {
		ListOperations<String, Object> valueOperations= this.redisTemplate.opsForList();
		List<Object> result = valueOperations.range(key, 0, -1);
		return result;
	}
	
	//Set
	public void putSet(String key,Object[] args) {
		this.redisTemplate.delete(key);
		this.redisTemplate.opsForSet().add(key, args);
	}
	
	public void putSetOne(String key,Object arg) {
		this.redisTemplate.opsForSet().add(key, arg);
	}
	
	public boolean isSetMember(String key,Object arg) {
		return this.redisTemplate.opsForSet().isMember(key, arg);
	}
	
	public Set<Object> getSet(String key) {
		Set<Object> result = this.redisTemplate.opsForSet().members(key);
		return result;
	}
	
	public long getSetSize(String key) {
		return this.redisTemplate.opsForSet().size(key);
	}
	
	//hash Map
	public void putMap(String key,Map<String, String> map) {
		this.redisTemplate.delete(key);
		this.redisTemplate.opsForHash().putAll(key, map);
	}
	
	public void putMapObj(String key,Map<Object, Object> map) {
		this.redisTemplate.delete(key);
		this.redisTemplate.opsForHash().putAll(key, map);
	}
	
	public void putMapOne(String key, String mapKey, String value) {
		this.redisTemplate.opsForHash().put(key, mapKey, value);
	}
	
	public Map<Object, Object> getMapAll(String key) {
		Map<Object, Object> result = this.redisTemplate.opsForHash().entries(key);
		return result;
	}
	
	//发布消息
	public void sendMsg(String topic, String msg) {
		this.redisTemplate.convertAndSend(topic, msg);
	}
	
}
